# 飞船类，要和飞船图片相关联，两者关联后，操作飞船类的对象就等于操作了飞船图片
import pygame
from pygame.sprite import Sprite

class Ship(Sprite):
    def __init__(self,ai_settings,screen):
        self.screen = screen
        self.ai_settings = ai_settings
        #关联图片
        self.image = pygame.image.load('images/ship.bmp')
        #获得图片的宽高
        self.rect = self.image.get_rect()
        # 获得屏幕的宽高
        self.screen_rect = screen.get_rect()
        #设置飞船位置
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        # 移动标志位
        self.moving_right = False
        self.moving_left = False
        super(Ship, self).__init__()


    def updata(self):
        # 如果不越界
        if self.moving_right==True and self.rect.right < 1000:
            self.rect.centerx=self.rect.centerx+self.ai_settings.ship_speed_factor
        if self.moving_left==True and self.rect.left > 0:
            self.rect.centerx=self.rect.centerx-self.ai_settings.ship_speed_factor

    def blitme(self): # 将图片绘制到窗口上
        self.screen.blit(self.image,self.rect)

    def center_ship(self):
        # """让飞船在屏幕上居中"""
        self.center = self.screen_rect.centerx
